package com.bc.bcapi.service;

import com.bc.bccore.zbconfig.ZbConfig;
import com.bc.bcmodel.consts.GlobalSettings;
import com.bc.bcmodel.domain.T_Mnguser_infoModel;
import com.bc.commoncore.security.AES;
import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.math.BigInteger;
import java.util.stream.Collectors;

import com.bc.bcapi.dao.T_Menu_infoMapper;
import com.bc.bcmodel.domain.T_Menu_infoModel;

/**
 * @Name T_Menu_infoService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_Menu_infoService {
    @Resource
    T_Menu_infoMapper t_Menu_infoMapper;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_Menu_infoModel req) {
        return t_Menu_infoMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_Menu_infoModel> req) {
        return t_Menu_infoMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_Menu_infoModel findByNamedParam(T_Menu_infoModel req) {
        return t_Menu_infoMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_Menu_infoModel> getAllList(T_Menu_infoModel req) {
        return t_Menu_infoMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_Menu_infoModel> getList(T_Menu_infoModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Menu_infoModel> list = t_Menu_infoMapper.findByNamedParamList(req);
        return new PageInfo<T_Menu_infoModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_Menu_infoModel req) {
        return t_Menu_infoMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_Menu_infoMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_Menu_infoModel req) {
        return t_Menu_infoMapper.count(req);
    }

    /*
     * 获取左侧菜单地址
     * **/

    public String getMenuStr(HttpServletRequest request) {
//        T_Mnguser_infoModel userInfo = UserContext.getCurrentContext().getLoginInfo();
        String cookieValue = CookieUtils.getCookie(request, GlobalSettings.MANAGE_COOKIENAME);
        String decryptValue = AES.aesDecrypt(cookieValue.trim());
        T_Mnguser_infoModel loginInfo = JsonUtil.str2obj(decryptValue, T_Mnguser_infoModel.class);
        String menu_ids = loginInfo.getRole_id();
        T_Menu_infoModel req = new T_Menu_infoModel();
        List<T_Menu_infoModel> list = t_Menu_infoMapper.findByNamedParamList(req);
        //一级分类
        List<T_Menu_infoModel> listOne = list.stream().filter(t -> t.getP_id().equals("")).collect(Collectors.toList());
        StringBuffer sb = new StringBuffer();
        for (T_Menu_infoModel item : listOne) {
            String one_menuid = item.getMenu_id();
            if (!menu_ids.contains(one_menuid + ",")
                    && !one_menuid.equals("M000000001")
                    ) {
                continue;
            }
            sb.append("<li>" +
                    "<p class='icon1'><a href=\"javaScript:void(0)\"><span></span>" + item.getMenu_name() + "</a></p>" +
                    "<ul>");
            List<T_Menu_infoModel> listTwo = list.stream().filter(t -> t.getP_id().equals(item.getMenu_id().toString())).collect(Collectors.toList());
            for (T_Menu_infoModel itemTwo : listTwo) {
                String two_menuid = itemTwo.getMenu_id();
                if (!menu_ids.contains(two_menuid + ",") &&
                        !two_menuid.equals("M000003002")) {
                    continue;
                }
                sb.append("<li><a href=\"" + ZbConfig.getSitePath() + itemTwo.getMenu_url() + "\">" + itemTwo.getMenu_name() + "</a></li>");
            }
            sb.append("</ul> </li>");
        }
        return sb.toString();
    }

    public String getMenuManageInfo() {
        List<T_Menu_infoModel> list = getAllList(null);
        StringBuilder sb = new StringBuilder();
        List<T_Menu_infoModel> listOne = list.stream().filter(t -> t.getP_id().equals("")).collect(Collectors.toList());
        for (T_Menu_infoModel item :
                listOne) {
            List<T_Menu_infoModel> listTwo = list.stream().filter(t -> t.getP_id().equals(item.getMenu_id())).collect(Collectors.toList());
            String status = item.getStatus().equals("0") ? "启用" : "禁用";
            sb.append("<tr>");
            sb.append("<td>" + item.getMenu_id() + "</td>");
            sb.append("<td>" + item.getMenu_name() + "</td>");
            sb.append("<td>--</td>");
            sb.append("<td>" + item.getMenu_url() + "</td>");
            sb.append("<td>" + item.getSort() + "</td>");
            sb.append("<td>" + status + "</td>");
            sb.append("<td>" + DateUtil.formartDate(item.getCreate_time(), "yyyy-MM-dd HH:mm:ss") + "</td>");
            sb.append("<td>" + DateUtil.formartDate(item.getModify_time(), "yyyy-MM-dd HH:mm:ss") + "</td>");
            sb.append("<td>" + item.getMemo() + "</td>");
            sb.append("<td>");
            sb.append("<a class=\"text-primary mr-10\" data-tab-title=\"添加二级菜单\" href=\"" + ZbConfig.getSitePath() + "/t_menu_info/add?menu_id=&p_id=" + item.getMenu_id() + "\">添加二级菜单</a>");
            sb.append("<a class=\"text-primary mr-10\" data-tab-title=\"修改\" href=\"" + ZbConfig.getSitePath() + "/t_menu_info/add?menu_id=" + item.getMenu_id() + "\">修改</a>");
            sb.append("<a class=\"text-primary mr-10 delBtn\" href=\"javascript:del('" + item.getMenu_id() + "')\">禁用/启用</a>");
            sb.append("</td></tr>");
            for (T_Menu_infoModel itemTwo : listTwo) {
                String statustwo = itemTwo.getStatus().equals("0") ? "启用" : "禁用";
                sb.append("<tr>");
                sb.append("<td>" + itemTwo.getMenu_id() + "</td>");
                sb.append("<td>--</td>");
                sb.append("<td>" + itemTwo.getMenu_name() + "</td>");
                sb.append("<td>" + itemTwo.getMenu_url() + "</td>");
                sb.append("<td>" + itemTwo.getSort() + "</td>");
                sb.append("<td>" + statustwo + "</td>");
                sb.append("<td>" + DateUtil.formartDate(itemTwo.getCreate_time(), "yyyy-MM-dd HH:mm:ss") + "</td>");
                sb.append("<td>" + DateUtil.formartDate(itemTwo.getModify_time(), "yyyy-MM-dd HH:mm:ss") + "</td>");
                sb.append("<td>" + itemTwo.getMemo() + "</td>");
                sb.append("<td>");
                sb.append("<a class=\"text-primary mr-10\" data-tab-title=\"修改\" href=\"" + ZbConfig.getSitePath() + "/t_menu_info/add?menu_id=" + itemTwo.getMenu_id() + "\">修改</a>");
                sb.append("<a class=\"text-primary mr-10 delBtn\" href=\"javascript:del('" + itemTwo.getMenu_id() + "')\">禁用/启用</a>");
                sb.append("</td></tr>");
            }
        }
        return sb.toString();
    }
}